package com.rf.richfitwheel.workflow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.WfProcessPageMapper;
import com.rf.richfitwheel.workflow.model.WfProcessPage;
import com.rf.richfitwheel.workflow.model.WfProcessPageExample;
import com.rf.richfitwheel.workflow.service.WfProcessPageService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("wfProcessPageService")
public class WfProcessPageServiceImpl implements WfProcessPageService {

    @Autowired
    private WfProcessPageMapper wfProcessPageMapper;

    @Override
    public void updateWfProcessPage(WfProcessPage wfProcessPage) {
        wfProcessPageMapper.updateByPrimaryKeySelective(wfProcessPage);
    }

    @Override
    public void addWfProcessPage(WfProcessPage wfProcessPage) {
        wfProcessPage.setId(UuidUtil.get32UUID());
        wfProcessPageMapper.insert(wfProcessPage);
    }

    @Override
    public WfProcessPage getById(String id) {
        return wfProcessPageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteByIds(List<String> ids) {
        for (String id : ids) {
            wfProcessPageMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void deleteById(String id) {
        wfProcessPageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Map<String, Object>> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        WfProcessPage wfProcessPage = (WfProcessPage) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), WfProcessPage.class);
        WfProcessPageExample example = new WfProcessPageExample();
        WfProcessPageExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(wfProcessPage.getFolderCode());
        List<WfProcessPage> list = wfProcessPageMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public WfProcessPage getByPageCode(String pageCode) {
        WfProcessPageExample example = new WfProcessPageExample();
        WfProcessPageExample.Criteria criteria = example.createCriteria();
        criteria.andPageCodeEqualTo(pageCode);
        List<WfProcessPage> list = wfProcessPageMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<WfProcessPage> getListByFolderCode(String folderCode) {
        WfProcessPageExample example = new WfProcessPageExample();
        WfProcessPageExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(folderCode);
        List<WfProcessPage> list = wfProcessPageMapper.selectByExample(example);
        return list;
    }

    @Override
    public WfProcessPage getByFolderCodeAndPageType(String folderCode, String pageType) {
        WfProcessPageExample example = new WfProcessPageExample();
        WfProcessPageExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(folderCode);
        criteria.andPageTypeEqualTo(pageType);
        List<WfProcessPage> list = wfProcessPageMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
